package tms.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import tms.controler.ChangeController;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JSeparator;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChangeView extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel contentPane;
    private ChangeController controller;
    private JTextField txtTicketNumber;
    private JButton btnAgree;
    private JLabel lblDeparts;
    private JLabel lblArrivers;
    private JLabel lblFlightDetail;
    private JButton btnSave;
    private ContactInfoPanel pnlContactInfo;

    public ChangeView(ChangeController controller) {
        setTitle("Change ticket");
        this.controller = controller;
        initComponents();
    }

    public void initComponents() {
        setPreferredSize(new Dimension(770, 430));
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new FormLayout(new ColumnSpec[] {
                FormSpecs.RELATED_GAP_COLSPEC,
                FormSpecs.DEFAULT_COLSPEC,
                FormSpecs.RELATED_GAP_COLSPEC,
                ColumnSpec.decode("max(30dlu;min)"),
                FormSpecs.RELATED_GAP_COLSPEC,
                FormSpecs.DEFAULT_COLSPEC,
                ColumnSpec.decode("default:grow"),
                FormSpecs.RELATED_GAP_COLSPEC,
                FormSpecs.DEFAULT_COLSPEC,
                FormSpecs.RELATED_GAP_COLSPEC,
                ColumnSpec.decode("default:grow"),
                FormSpecs.RELATED_GAP_COLSPEC,
                FormSpecs.DEFAULT_COLSPEC,
                FormSpecs.RELATED_GAP_COLSPEC,
                ColumnSpec.decode("min:grow"),
                FormSpecs.RELATED_GAP_COLSPEC,
                FormSpecs.DEFAULT_COLSPEC,
                FormSpecs.RELATED_GAP_COLSPEC,
                FormSpecs.DEFAULT_COLSPEC},
            new RowSpec[] {
                FormSpecs.RELATED_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.PARAGRAPH_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.RELATED_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.RELATED_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.RELATED_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.PARAGRAPH_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.RELATED_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.RELATED_GAP_ROWSPEC,
                FormSpecs.DEFAULT_ROWSPEC,
                FormSpecs.PARAGRAPH_GAP_ROWSPEC,
                RowSpec.decode("max(16dlu;min)")}));

        JLabel lblToMakeSure = Tools.createLabel("To make sure you want to change" +
                " your ticket, we require you provide the ticket number:");
        contentPane.add(lblToMakeSure, "2, 2, 14, 1, left, default");

        txtTicketNumber = Tools.createTextField("");
        contentPane.add(txtTicketNumber, "17, 2, fill, fill");
        txtTicketNumber.setColumns(10);

        btnAgree = Tools.createButton("Agree");
        btnAgree.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                controller.getInfoFromTicketNum(txtTicketNumber.getText());
            }
        });

        contentPane.add(btnAgree, "19, 2, default, fill");

        JLabel lblDepatureFlight = Tools.createLabel("Flight information");
        contentPane.add(lblDepatureFlight, "2, 4, 14, 1");

        contentPane.add(new JSeparator(), "2, 6, 18, 1");

        JLabel lblDepartsCaption = Tools.createLabel("Departs");
        contentPane.add(lblDepartsCaption, "6, 8");

        JLabel lblArriver = Tools.createLabel("Arrivers");
        contentPane.add(lblArriver, "9, 8");

        JLabel lblFlightDetailCaption = Tools.createLabel("Flight detail");
        contentPane.add(lblFlightDetailCaption, "13, 8");

        lblDeparts = Tools.createLabel("_");
        contentPane.add(lblDeparts, "6, 10, 2, 1");

        lblArrivers = Tools.createLabel("_");
        contentPane.add(lblArrivers, "9, 10, 3, 1");

        lblFlightDetail = Tools.createLabel("_");
        contentPane.add(lblFlightDetail, "13, 10, 3, 1");

        JLabel lblReservationContactInformation =
                Tools.createLabel("Reservation Contact Information");
        contentPane.add(lblReservationContactInformation, "2, 12, 14, 1");

        contentPane.add(new JSeparator(), "2, 14, 18, 1");

        pnlContactInfo = new ContactInfoPanel();
        contentPane.add(pnlContactInfo, "4, 16, 16, 1, fill, fill");

        btnSave = Tools.createButton("Save");
        contentPane.add(btnSave, "2, 18, 3, 1, default, fill");
        btnSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (controller.save()) {
                    JOptionPane.showMessageDialog(null, "Update succesfully!");
                } else {
                    JOptionPane.showMessageDialog(null,
                            "Update not succesfully!");
                }
            }
        });


        JButton btnCancelTicket = Tools.createButton("Cancel ticket");
        btnCancelTicket.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                controller.cancelTicket();
            }
        });
        contentPane.add(btnCancelTicket, "6, 18, default, fill");

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        Tools.centerFrame(this);
    }

    public void closeMe() {
        this.dispose();
    }

    public void setDeparts(String content) {
        lblDeparts.setText(content);
    }

    public void setArrivers(String content) {
        lblArrivers.setText(content);
    }

    public void setFlightDetail(String content) {
        lblFlightDetail.setText(content);
    }

    public ContactInfoPanel getContactInfoPanel() {
        return pnlContactInfo;
    }

}
